<template>
  <div class="app-container">
    <h3>Banner表单</h3>
    <el-form ref="form" :model="bannerForm" label-width="80px">
      <el-form-item label="标题">
        <el-input v-model="bannerForm.title"></el-input>
      </el-form-item>
      <el-form-item label="图片">
        <el-upload
          class="avatar-uploader"
          :action="BASE_API+'/oss/file/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="访问路径">
        <el-input v-model="bannerForm.linkUrl"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate">{{text}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import banner from "@/api/banner";

  export default {
    name: "form",
    data() {
      return {
        bannerForm: {},//banner表单
        imagecropperShow: false,//是否显示
        imagecropperKey: 0,//图片上传key值
        BASE_API: process.env.BASE_API,//后端接口地址
        saveBtnDisabled: false,//保存按钮是否禁用
        text: '保存',//按钮文字
        imageUrl: '',//图片地址
      }
    },
    created() {
      this.init()
    },
    watch: {
      $route(to, from) {//监听，路由发生变化，方法就会执行
        this.init()
      }
    },
    methods: {
      //初始化表单信息
      init() {
        if (this.$route.params && this.$route.params.id) {
          this.bannerForm.id = this.$route.params.id//获取id
          // console.log("=========" + this.bannerForm.id)
          this.getBanner()//初始化表单信息
        } else {
          //清空表单
          this.bannerForm = {}
        }
      },
      //数据回显
      getBanner() {
        banner.getBannerById(this.bannerForm.id)
          .then(response => {
            this.bannerForm = response.data.banner
            this.imageUrl = this.bannerForm.imageUrl
          })
      },
      //修改
      update() {
        //禁用按钮
        this.saveBtnDisabled = true
        banner.updateBanner(this.bannerForm)
          .then(response => {
            //提示
            this.$message({
              type: 'success',
              message: response.message
            })
            this.clearForm()
            //跳转到列表页面
            this.$router.push({path: '/cms/banner/list'})
          })
          .catch(response => {
            //提示
            this.$message({
              type: 'error',
              message: response.message
            })
          })
      },
      //保存
      save() {
        //禁用按钮
        this.saveBtnDisabled = true
        banner.addBanner(this.bannerForm)
          .then(response => {
            //提示
            this.$message({
              type: 'success',
              message: response.message
            })
            this.clearForm()
            //跳转到列表页面
            this.$router.push({path: '/cms/banner/list'})
          })
          .catch(response => {
            //提示
            this.$message({
              type: 'error',
              message: response.message
            })
          })
      },
      //保存的方法
      saveOrUpdate() {
        //判断是添加还是修改
        if (this.bannerForm.id) {//修改
          this.update()
        } else {//增加
          this.save()
        }
      },
      //清空表单
      clearForm() {
        this.saveBtnDisabled = false//按钮不禁用
        this.bannerForm = {}//清空表单
      },
      //上传头像成功
      handleAvatarSuccess(res, file) {
        console.log("+++++++++++", res)
        this.imageUrl = res.data.url
        this.bannerForm.imageUrl = this.imageUrl
        console.log("==========", this.imageUrl)
      },
      //上传头像之前
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
    }
  }
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    /*width: 178px;*/
    /*height: 178px;*/
    display: block;
  }
</style>
